﻿using pe.edu.pucp.lp2.controller;
using pe.edu.pucp.lp2.model.bean;
using SalesModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SalesApp
{
    public partial class EmployeeForm : Form
    {
        public EmployeeForm()
        {
            InitializeComponent();
        }
        public void RefreshGrid()
        {
            dgvEmployee.Rows.Clear();

            List<Cashier> list1 = SalesManager.getAllCashier();

            for (int i = 0; i < list1.Count; i++)
            {
                dgvEmployee.Rows.Add(new String[] { ""+list1[i].getId(),
                                                         list1[i].getDni(),
                                                         list1[i].getName(),
                                                         list1[i].getmLastName(),
                                                         list1[i].getfLastName(),
                                                         ""+list1[i].getBirthdate()
                                                         
                });
            }
            /*List<StoreManager> list2 = SalesManager.getAllStoreManager();

            for (int i = 0; i < list2.Count; i++)
            {
                dgvEmployee.Rows.Add(new String[] { ""+list2[i].getId(),
                                                         list2[i].getDni(),
                                                         list2[i].getName(),
                                                         list2[i].getmLastName(),
                                                         list2[i].getfLastName(),
                                                         ""+list2[i].getBirthdate()                                                       
                });
            }*/
        }
        private void EmployeeForm_Load(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int id = Int32.Parse(txtId.Text);
            String name = txtName.Text;
            String fLastName = txtmLastName.Text;
            String mLastName = txtpLastName.Text;
            String dni = txtDNI.Text;
            int storeId = Int32.Parse(txtStoreId.Text);
            int userCode = Int32.Parse(txtUserName.Text);
            int password = Int32.Parse(txtPassword.Text);
            DateTime nacDate = DateTime.Parse(txtNacDate.Text);
                 

            if (cmdType.SelectedItem == "Cajero")
            {
                Cashier c = new Cashier();
                c.setId(id);
                c.setName(name);
                c.setmLastName(mLastName);
                c.setfLastName(fLastName);
                c.setBirthdate(nacDate);
                c.setDni(dni);
                c.setUserCode(userCode);
                c.setPassword(password);
                Store store = new Store();
                store.setId(storeId);
                c.setStore(store);
                SalesManager.addCashier(c);
            }
            else if (cmdType.SelectedItem == "Administrador")
            {
                StoreManager s = new StoreManager();
                s.setId(id);
                s.setName(name);
                s.setmLastName(mLastName);
                s.setfLastName(fLastName);
                s.setBirthdate(nacDate);
                s.setDni(dni);
                s.setUserCode(userCode);
                s.setPassword(password);
                Store store = new Store();
                store.setId(storeId);
                s.setStore(store);
                SalesManager.addStoreManager(s);
            }
            
            RefreshGrid();
        }
        
        /*
        private void dgvEmployee_Click(object sender, EventArgs e)
        {
            int selRow = dgvEmployee.SelectedRows[0].Index;
            int emId = Int32.Parse(dgvEmployee.Rows[selRow].Cells[0].Value.ToString());

            Employee em = SalesManager.getCashierById(emId);

            if (em == null)
            {
                em = SalesManager.getManagerById(emId);
            }

            txtDNI.Text = em.getDni();
            txtId.Text = ""+em.getId();
            txtmLastName.Text = em.getfLastName();
            txtpLastName.Text = em.getmLastName();
            txtNacDate.Text = "" + em.getBirthdate();
        }
        */
        private void btnClear_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;

            txtId.Text = "";
            txtName.Text = "";
            txtpLastName.Text = "";
            txtmLastName.Text = "";
            txtNacDate.Text = "" + dt;
            txtDNI.Text = "";
            txtStoreId.Text = "";
            txtUserName.Text = "";
            txtPassword.Text = "";
        }

        private void dgvEmployee_Click(object sender, EventArgs e)
        {
            int selRow = dgvEmployee.SelectedRows[0].Index;
            int cashierId = Int32.Parse(dgvEmployee.Rows[selRow].Cells[0].Value.ToString());
            Cashier c = SalesManager.getCashierById(cashierId);

            txtId.Text = "" + c.getId();
            txtName.Text = c.getName();
            txtpLastName.Text = c.getfLastName();
            txtmLastName.Text = c.getmLastName();
            txtDNI.Text = c.getDni();
            txtNacDate.Text = "" + c.getBirthdate();
            txtUserName.Text = "" + c.getUserCode();
            txtPassword.Text = "" + c.getPassword();
            txtStoreId.Text = "" + c.getStore().getId();
            
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int id = Int32.Parse(txtId.Text);
            SalesManager.deleteCashier(id);
            RefreshGrid();
        }

        private void dgvEmployee_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
